package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.string.StringUtils;

public class CrossUnionReqContext {

    long serverId;

    long playerId;

    int ret;

    CrossUnionInfo unionInfo;

    CrossUnionMember member;

    CrossUserBaseInfo userBaseInfo;

    public CrossUnionReqContext(long serverId, long playerId) {
        this.serverId = serverId;
        this.playerId = playerId;
    }

    public static CrossUnionReqContext initContext(long serverId, long playerId){
        CrossUnionReqContext context = new CrossUnionReqContext(serverId, playerId);

        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(playerId);
        if(crossUserBaseInfo == null){
            context.ret = GameErrorCode.E_USER_NO_FOUND;
            return context;
        }

        context.userBaseInfo = crossUserBaseInfo;

        return context;
    }

    public CrossUnionReqContext loadUnionMember(){
        if(userBaseInfo == null){
            return this;
        }
        member = CrossUnionMgr.getUnionMember(userBaseInfo.getUserBaseInfo().getUnionUid(), playerId);
        if(member == null || StringUtils.isNullOrEmpty(member.getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return this;
        }

        return this;
    }

    public CrossUnionReqContext loadUnionInfo(){
        if(member != null && !StringUtils.isNullOrEmpty(member.getUnionUid())) {
            unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
            if (unionInfo == null) {
                ret = GameErrorCode.E_UNION_NO_FOUND;
                return this;
            }
        }
        return this;
    }


    public long getServerId() {
        return serverId;
    }

    public void setServerId(long serverId) {
        this.serverId = serverId;
    }

    public long getPlayerId() {
        return playerId;
    }

    public void setPlayerId(long playerId) {
        this.playerId = playerId;
    }

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public CrossUnionInfo getUnionInfo() {
        return unionInfo;
    }

    public void setUnionInfo(CrossUnionInfo unionInfo) {
        this.unionInfo = unionInfo;
    }

    public CrossUnionMember getMember() {
        return member;
    }

    public void setMember(CrossUnionMember member) {
        this.member = member;
    }

    public CrossUserBaseInfo getUserBaseInfo() {
        return userBaseInfo;
    }

    public void setUserBaseInfo(CrossUserBaseInfo userBaseInfo) {
        this.userBaseInfo = userBaseInfo;
    }
}
